package edu.nuc.event.controller;


import edu.nuc.event.pojo.Competition;
import edu.nuc.event.pojo.Match;
import edu.nuc.event.pojo.User;
import edu.nuc.event.service.ComService;
import edu.nuc.event.service.MatchService;
import edu.nuc.event.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MatchController {

    @Autowired
    private MatchService matchService;

    @Autowired
    private ComService comService;

    @Autowired
    private UserService userService;

    @GetMapping("/comprize")
    public String comprize(@RequestParam("id") int id, Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginuser");
        if (user.getCpid() == 0) {
            return "redirect:/noplay";
        } else {
            List<Match> matches = matchService.matchsByComId(id);
            int account = matchService.demandaccountByComId(id);
            Competition competition = comService.selectComByid(id);
            String comname = competition.getCpname();

            model.addAttribute("matches", matches);
            model.addAttribute("account", account);
            model.addAttribute("comname", comname);

            return "tocomprize";
        }
    }

    @GetMapping("/toadd-match")
    public String toaddmatch(HttpSession session, Model model) {
        User user = (User) session.getAttribute("loginuser");
        Match match = matchService.findmatchbyuid(user.getId());
        model.addAttribute("match", match);

        model.addAttribute("user", user);
        Competition com = comService.selectComByid(user.getCpid());
        model.addAttribute("com", com);
        //若用户没有参加赛事，返回到参加赛事界面
        if (user.getCpid() == 0) {
            return "redirect:/noplay";
        } else if (null == match) {//若用户参加赛事，但是没有提交作品，则跳转到增加作品页面
            return "toadd-match";
        } else {//用户参加了赛事，同时也提交过作品，则跳转到修改作品页面
            return "toupdate-match";//
        }
    }

    @PostMapping("/doadd-match")
    public String doaddmatch(Match match) {

        int i = matchService.addmatch(match.getUserid(), match.getWord());

        return "redirect:/todetails4";
    }

    @PostMapping("/doupdate-match")
    public String doupdatematch(Match match) {

        int i = matchService.updatematch(match);

        return "redirect:/todetails4";
    }

    @GetMapping("/tomatch")
    public String tomatch(Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginuser");
        if (user.getCpid() == 0) {
            return "redirect:/noplay";
        } else {
            Competition competition = comService.selectComByid(user.getCpid());
            String comname = competition.getCpname();

            model.addAttribute("comname", comname);

            List<User> users = matchService.selectmatchbycom(user.getCpid());

            model.addAttribute("users", users);

            int account = userService.demanduseraccount(user.getCpid(), 3);
            model.addAttribute("account", account);
        }


        return "toselect-match";
    }

    @GetMapping("/toupdate")
    public String toupdate(@RequestParam("id") int id, Model model, HttpSession session) {
        User loginuser = (User) session.getAttribute("loginuser");

        model.addAttribute("loginuser", loginuser);

        Competition competition = comService.selectComByid(loginuser.getCpid());
        String cpname = competition.getCpname();

        model.addAttribute("comname", cpname);

        Match match = matchService.selectmatchbyid(id);

        model.addAttribute("match", match);

        return "toupdate-prize";
    }

    @PostMapping("/doupdate-prize")
    public String doupdatematchs(Match match) {

        int i = matchService.updateprize(match);
        return "redirect:/todetails4";
    }
}
